const app = getApp();
const companyName = app.globalData.companyName;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    appointmentList: [],
    page: 1,
    pageSize: 20
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.fatchAppointmentList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  fatchAppointmentList: function() {
    const that = this;
    const appointmentList = that.data.appointmentList;
    const url = app.globalData.url;
    const page = that.data.page;
    const pageSize = that.data.pageSize;
    wx.showNavigationBarLoading();
    wx.request({
      url: url + '/management/queryAppointment',
      data: {
        companyName,
        page,
        pageSize
      },
      method: 'POST',
      success: function(response) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        if (response.data.success) {
          const content = response.data.content;
          if (content && content.length > 0) {
            content.forEach(function(item) {
              appointmentList.push(item);
            });
            that.setData({
              appointmentList
            });
          } else {
            wx.showToast({
              title: '没有更多数据了~',
              icon: 'none'
            })
          }
        } else {
          wx.showToast({
            title: response.data.message,
            icon: 'none'
          })
        }
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      pageSize: 20,
      appointmentList: []
    });
    this.fatchAppointmentList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    const page = this.data.page;
    this.setData({
      page: page + 1,
      pageSize: 20
    });
    this.fatchAppointmentList();
  },
})